const app = getApp();
const util = require('../../../utils/util.js')
Page({
  data: {
    fields: [
      {
        prop: 'userName',
        label: '姓名',
        width: 160
      },
      {
        prop: 'levelFourOrgName',
        label: '所属组织',
        width: 160
      },
      {
        prop: 'num',
        label: '活跃次数',
        width: 150
      },
      {
        prop: 'lastTime',
        label: '最后活跃时间',
        width: 160
      }
    ],
    list: [],
    queryForm: '',
    sortField: '',
    startdate: (util.timestampToTime(Date.now() - 7 * 60 * 60 * 24 * 1000)),
    enddate: (util.timestampToTime(Date.now() - 1 * 60 * 60 * 24 * 1000)),
    query: { bankName: '', bankBranchName: '', bankSubBranchName: '', bankWorkstationName: '' },
    queryForm: { levelOneOrgId: '', levelTwoOrgId: '', levelThreeOrgId: '', levelFourOrgId: '', startDate: '', endDate: ''},
    bankindex: '',
    branchidindex: '',
    subBranchindex:'',
    siteindex:'',
    bankList:[],
    bankidList:[],
    params: { parentId: ''},
    currentPage:1
  },
  onLoad: function (options) {
    let queryForm = JSON.parse(options.queryForm)
    let queryIndex = JSON.parse(options.queryIndex)
    this.setData({
      queryForm: queryForm,
      startdate: queryForm.startDate,
      enddate: queryForm.endDate,
      bankindex: queryIndex.bankindex,
      branchidindex: queryIndex.branchidindex,
      subBranchindex: queryIndex.subBranchindex,
      siteindex: queryIndex.siteindex,
    })
  },
  onShow: function () {
    this.activeList()
    this.getAllBankList()
    if (this.data.queryForm.levelOneOrgId) {
      this.defaultBranch()
    }
    if (this.data.queryForm.levelTwoOrgId) {
      this.defaultSubBranch()
    }
    if (this.data.queryForm.levelThreeOrgId) {
      this.defaultSite()
    }
  },
  activeList () {
    const params = Object.assign({currentPage: this.data.currentPage, pageSize: 15}, this.data.queryForm)
    app.api.post({
      url: 'report_activity_detailPage',
      data: params
    }).then(res => {
      console.log(res)
      let { list ,currentPage } = this.data
      currentPage === 1 ? (list = res.list) : list.push(...res.list)
      this.setData({
        list: list,
      })
    })
  },
  upClick (row) {
    this.setData({sortField: row.detail.row.prop, sortType: 'asc'})
    this.activeList()
  },
  downClick (row) {
    this.setData({sortField: row.detail.row.prop, sortType: 'desc'})
    this.activeList()
  },
   // 开始时间
   bindstartDateChange: function(e) {
    this.setData({
      startdate: e.detail.value
    })
    this.data.queryForm.startDate = e.detail.value
    this.setData({
      queryForm: this.data.queryForm
    })
    this.setData({
      currentPage:1
    })
    this.activeList()
  },
  // 结束时间
  bindendDateChange: function(e) {
    this.setData({
      enddate: e.detail.value
    })
    this.data.queryForm.endDate = e.detail.value
    this.setData({
      queryForm: this.data.queryForm
    })
    this.setData({
      currentPage:1
    })
    this.activeList()
  },
  // 获取所有银行
  getAllBankList () {
    app.api.post({
      url: 'userAccessableBank_getAccessableBank',
      data: {}
    }).then(res => {
      // 银行
      let bankList = []
      let bankidList = []
      if (res.length > 0) {
        res.map(item => {
          bankList.push(item.orgName)
          bankidList.push(item.id)
        })
      }
      this.setData({
        bankList: bankList,
        bankidList: bankidList,
      })
    })
  },
  defaultBranch () {
    app.api.post({
      url: 'userAccessableBank_getAccessableBankOrgChildrenByParentId',
      data: {parentId: this.data.queryForm.levelOneOrgId}
    }).then(res => {
      if (res.length > 0) {
        this.data.branchList = []
        this.data.branchidList = []
        res.map(item => {
            this.data.branchList.push(item.orgName)
            this.data.branchidList.push(item.id)
          })
      }
      this.setData({
        branchList: this.data.branchList,
        branchidList: this.data.branchidList,
      })
    })
  },
  defaultSubBranch () {
    app.api.post({
      url: 'userAccessableBank_getAccessableBankOrgChildrenByParentId',
      data: {parentId: this.data.queryForm.levelTwoOrgId}
    }).then(res => {
      if (res.length > 0) {
        this.data.subBranchList = []
        this.data.subBranchidList = []
        res.map(item => {
            this.data.subBranchList.push(item.orgName)
            this.data.subBranchidList.push(item.id)
          })
      }
      this.setData({
        subBranchList: this.data.subBranchList,
        subBranchidList: this.data.subBranchidList
      })
    })
  },
  defaultSite () {
    app.api.post({
      url: 'userAccessableBank_getAccessableBankOrgChildrenByParentId',
      data: {parentId: this.data.queryForm.levelThreeOrgId}
    }).then(res => {
      if (res.length > 0) {
        this.data.siteList = []
        this.data.siteidList = []
        res.map(item => {
            this.data.siteList.push(item.orgName)
            this.data.siteidList.push(item.id)
          })
      }
      this.setData({
        siteList: this.data.siteList,
        siteidList: this.data.siteidList
      })
    })
  },
  bankChange: function(e) {
    this.data.queryForm.levelOneOrgId = this.data.bankidList[e.detail.value]
    this.data.params.parentId = this.data.bankidList[e.detail.value]
    this.setData({
      bankindex: e.detail.value,
      params: this.data.params
    })
    app.api.post({
      url: 'userAccessableBank_getAccessableBankOrgChildrenByParentId',
      data: this.data.params
    }).then(res => {
      if (res.length > 0) {
        this.data.branchList = []
        this.data.branchidList = []
        res.map(item => {
            this.data.branchList.push(item.orgName)
            this.data.branchidList.push(item.id)
          })
      } else {
        this.data.branchList = []
        this.data.subBranchList = []
        this.data.siteList = []
      }
      this.data.queryForm.levelTwoOrgId = ''
      this.data.queryForm.levelThreeOrgId = ''
      this.data.queryForm.levelFourOrgId = ''
      this.setData({
        branchList: this.data.branchList,
        subBranchList: this.data.subBranchList,
        siteList: this.data.siteList,
        branchidList: this.data.branchidList,
        branchidindex: '',
        subBranchindex: '',
        siteindex: '',
        queryForm: this.data.queryForm
      }, () => {
        console.log('赋值成功')
        this.setData({
          currentPage:1
        })
        this.activeList()
      })
    })
  },
  branchChange: function(e) {
    this.data.queryForm.levelTwoOrgId = this.data.branchidList[e.detail.value]
    this.data.params.parentId = this.data.branchidList[e.detail.value]
    this.setData({
      branchidindex: e.detail.value,
      params: this.data.params
    })
    app.api.post({
      url: 'userAccessableBank_getAccessableBankOrgChildrenByParentId',
      data: this.data.params
    }).then(res => {
      if (res.length > 0) {
        this.data.subBranchList = []
        this.data.subBranchidList = []
        res.map(item => {
            this.data.subBranchList.push(item.orgName)
            this.data.subBranchidList.push(item.id)
          })
      } else {
        this.data.subBranchList = []
        this.data.siteList = []
      }
      this.data.queryForm.levelThreeOrgId = ''
      this.data.queryForm.levelFourOrgId = ''
      this.setData({
        queryForm: this.data.queryForm,
        subBranchindex: '',
        siteindex: '',
        subBranchList: this.data.subBranchList,
        siteList: this.data.siteList,
        subBranchidList: this.data.subBranchidList
      }, () => {
        this.setData({
          currentPage:1
        })
        this.activeList()
      })
    })
  },
  subBranchChange: function(e) {
    console.log(e, this.data.subBranchList)
    this.data.queryForm.levelThreeOrgId = this.data.subBranchidList[e.detail.value]
    this.data.params.parentId = this.data.subBranchidList[e.detail.value]
    this.setData({
      subBranchindex: e.detail.value,
      params: this.data.params
    })
    app.api.post({
      url: 'userAccessableBank_getAccessableBankOrgChildrenByParentId',
      data: this.data.params
    }).then(res => {
      if (res.length > 0) {
        this.data.siteList = []
        this.data.siteidList = []
        res.map(item => {
            this.data.siteList.push(item.orgName)
            this.data.siteidList.push(item.id)
          })
      } else {
        this.data.siteList = []
      }
      this.data.queryForm.levelFourOrgId = ''
      this.setData({
        queryForm: this.data.queryForm,
        siteindex: '',
        siteList: this.data.siteList,
        siteidList: this.data.siteidList
      }, () => {
        this.setData({
          currentPage:1
        })
        this.activeList()
      })
    })
  },
  siteChange: function(e) {
    this.data.queryForm.levelFourOrgId = this.data.siteidList[e.detail.value]
    this.setData({
      siteindex: e.detail.value,
      queryForm: this.data.queryForm
    })
    this.setData({
      currentPage:1
    })
    this.activeList()
  },
  // 页面上拉触底事件（上拉加载更多）
  onReachBottom: function () {
    let _currentPage = this.data.currentPage
    _currentPage++
    this.setData({
      currentPage:_currentPage
    })
    this.activeList();//后台获取新数据并追加渲染
 },
})